#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int dat[6] = { 0 };		   //存储0字符，1单词，2句子，3代码行，4空行，5注释行
char txtaddress[50];       //文件地址
char mode;                 //c字符，w单词，d句子，i代码行，f空行，g注释行，m全部
int comlines = 0;          //注释行标志位
int ifblank = 0;           //空行标志位


int dischar(fstream& outtxt, int* dat)  //统计函数
{
	char str[50];         
	while (outtxt.getline(str, 50))
	{
		for (int i = 0; i < strlen(str); i++) //统计字符数
			if ((str[i] >= 'A' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') ||
				str[i] == '.' || str[i] == ',' || str[i] == '?' || str[i] == '!' || str[i] == '/' || str[i] == '*')
			{
				dat[0]++;
				ifblank++;
			} 
		if (ifblank == 0)         
			dat[4]++;
		else if (ifblank != 0)    
			dat[3]++;
		ifblank = 0;         //空行标志复位
	}
	return 0;
}


int disword(fstream& outtxt, int* dat)  //统计函数
{
	char str[50];         
	while (outtxt.getline(str, 50))
	{
		for (int i = 0; i < strlen(str); i++) //统计单词数
			if (str[i]>= 'A' && str[i]<= 'z')
				if (str[i+1] <= 'A' || str[i+1] >= 'z')
					dat[1]++;
	}
	return 0;
}

int dissent(fstream& outtxt, int* dat)  //统计函数
{
	char str[50];          
	while (outtxt.getline(str, 50))
	{
		for (int i = 0; i < strlen(str); i++)//统计句子
			if ((str[i] == '.' && str[i + 1] != '.')|| (str[i] == '!' && str[i + 1] != '!' ))
				dat[2]++;
	}
	return 0;
}

int disline(fstream& outtxt, int* dat)  //统计函数
{
	char str[50];             
	while (outtxt.getline(str, 50))
	{
		for (int i = 0; i < strlen(str); i++)
		{
			if (str[i] == '/' && str[i + 1] == '/' && str[i + 2] != '/')//统计"//"注释行
				dat[5]++;
			if (str[i] == '/' && str[i + 1] == '*')//注释行开始         //统计“/* */”注释行
				comlines = 1; 
			if (str[i] == '*' && str[i + 1] == '/')//注释行结束
			{
				comlines = 0; 
				dat[5]++;
			} 
		}
		if (comlines == 1)   //注释行未结束
			dat[5]++;
	}
	return 0;
}

int display(fstream& outtxt, int* dat)  //统计函数
{
	char str[50];             
	while (outtxt.getline(str, 50))
	{
		for (int i = 0; i < strlen(str); i++)
		{
			if ((str[i] >= 'A' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') ||  //统计字符数
				str[i] == '.' || str[i] == ',' || str[i] == '?' || str[i] == '!' || str[i] == '/' || str[i] == '*')
			{
				dat[0]++;
				ifblank++;   
			}
			if (str[i] >= 'A' && str[i] <= 'z')   //统计单词数
				if (str[i + 1] <= 'A' || str[i + 1] >= 'z')
					dat[1]++;
			if ((str[i] == '.' && str[i + 1] != '.') )//统计句子数
				dat[2]++;
			if (str[i] == '/' && str[i + 1] == '/' && str[i + 2] != '/')//统计"//"注释行
				dat[5]++;
			if (str[i] == '/' && str[i + 1] == '*')//注释行开始         //统计“/* */”注释行
				comlines = 1; //标志位打开
			if (str[i] == '*' && str[i + 1] == '/')//注释行结束
			{
				comlines = 0; //标志位关闭
				dat[5]++;
			}
		}
		if (comlines == 1)   //注释行未结束
			dat[5]++;
		if (ifblank == 0)         //统计空行
			dat[4]++;
		else if (ifblank != 0)    //统计行数
			dat[3]++;
		ifblank = 0;        
	}
	return 0;
}


int main()
{
	while (1)
	{
		cout << "wc.exe -";
		cin >> mode;                             //选择模式
		cin >> txtaddress;                       //写入地址

		fstream outtxt(txtaddress, ios::in);     //读取文本
		if (mode == 'c')
		{
			dischar(outtxt, dat);                      //字符统计函数
			cout << "characters: " << dat[0] << endl;
		}
		else if (mode == 'w')
		{
			disword(outtxt, dat);                      //单词统计函数
			cout << "words:" << dat[1] << endl;
		}
		else if (mode == 'd')
		{
			dissent(outtxt, dat);                      //句子统计函数
			cout << "sentence:" << dat[2] << endl;
		}
		else if (mode == 'i')
		{
			dischar(outtxt, dat);                      //代码行统计函数
			cout << "lines:" << dat[3] << endl;
		}
		else if (mode == 'g')
		{
			dischar(outtxt, dat);                      //空行统计函数
			cout << "blanklines:" << dat[4] << endl;
		}
		else if (mode == 'f')
		{
			disline(outtxt, dat);                      //注释行统计函数
			cout << "commentlines:" << dat[5] << endl;
		}
		else if (mode == 'm')
		{
			display(outtxt, dat);                     //统计函数
			cout << "characters: " << dat[0] << endl;
			cout << "words:" << dat[1] << endl;
			cout << "sentence:" << dat[2] << endl;
			cout << "lines:" << dat[3] << endl;
			cout << "blanklines:" << dat[4] << endl;
			cout << "commentlines:" << dat[5] << endl;
		}
		cout << endl;
		outtxt.close();
	}
	system("pause");
	return 0;

}